package com.jml.netty.marshing2;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHandler  extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
			//do something msg
		Req req = (Req)msg;
		System.out.println(req.toString());
		Resp resp = new Resp();
		resp.setId(req.getId());
		resp.setName("resp"+req.getName());

		/*String writepath = System.getProperty("user.dir")+File.separatorChar+"receive"+File.separatorChar+UUID.randomUUID() +"006.jpg";
		byte[] bytes = GzipUtils.ungzip(req.getAttachment());
		FileOutputStream fos = new FileOutputStream(writepath);
		fos.write(bytes);
		fos.close();*/

		ctx.writeAndFlush(resp);
		//.addListener(ChannelFutureListener.CLOSE);

	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

}
